/**
 * HP Subscriber, Network, and Application Policy
 *
 * Copyright (C) 2009-2014 Hewlett-Packard Company
 */
package com.hp.snap.gr.model;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>DeployStatus</p>
 *
 * @author Joey Yi
 */
public class DeployStatus {

    /**
     * Deployment unique name
     */
    private final String deployName;

    /**
     * Deployment role
     */
    private final DeployRole deployRole;

    /**
     * GR manager status
     */
    private final GRStatus grStatus;

    /**
     * Deployment configured global business service status
     */
    private final BizServiceStatus globalBizServiceStatus;

    private boolean failed;

    /**
     * Deployment session server status, map key is the session server type, and the value is the session server list of this type
     */
    private final Map<String, List<SessionServerStatus>> sessionServerStatusMap;

    public DeployStatus(String deployName, DeployRole deployRole, GRStatus grStatus, BizServiceStatus globalBizServiceStatus,
                        Map<String, List<SessionServerStatus>> sessionServerStatusMap) {
        this.deployName = deployName;
        this.deployRole = deployRole;
        this.grStatus = grStatus;
        this.globalBizServiceStatus = globalBizServiceStatus;
        this.sessionServerStatusMap = sessionServerStatusMap;
    }

    public String getDeployName() {
        return deployName;
    }

    public DeployRole getDeployRole() {
        return deployRole;
    }

    public GRStatus getGrStatus() {
        return grStatus;
    }

    public BizServiceStatus getGlobalBizServiceStatus() {
        return globalBizServiceStatus;
    }

    public Map<String, List<SessionServerStatus>> getSessionServerStatusMap() {
        return sessionServerStatusMap;
    }

    /**
     * Read the session server status by session server type and business service status
     *
     * @param ssType           session server type
     * @param bizServiceStatus Business service status
     * @return business service status
     */
    public List<SessionServerStatus> filterSSStatusByTypeAndBizStatus(String ssType, BizServiceStatus bizServiceStatus) {
        if (StringUtils.isEmpty(ssType)) throw new IllegalArgumentException("ssType cannot be empty!");

        if (bizServiceStatus == null) throw new IllegalArgumentException("bizServiceStatus cannot be empty!");

        List<SessionServerStatus> filterSessionServerStatusList = new ArrayList<SessionServerStatus>();

        if (sessionServerStatusMap == null || sessionServerStatusMap.isEmpty()) {
            return filterSessionServerStatusList;
        }

        List<SessionServerStatus> sessionServerStatusList = sessionServerStatusMap.get(ssType);

        for (SessionServerStatus sessionServerStatus : sessionServerStatusList) {
            if (sessionServerStatus.getProcessStatus() == SessionServerStatus.ProcessStatus.STOPPED) {
                continue;
            }

            if (sessionServerStatus.getKernelStatus() != SessionServerStatus.KernelStatus.ACTIVE) {
                continue;
            }

            if (sessionServerStatus.getBizServiceStatus() == bizServiceStatus) {
                filterSessionServerStatusList.add(sessionServerStatus);
            }
        }

        return filterSessionServerStatusList;
    }

    /**
     * @param bizServiceStatus
     * @return
     */
    public Map<String, Integer> filterSessionServerWithBizStatus(BizServiceStatus bizServiceStatus) {
        if (bizServiceStatus == null) throw new IllegalArgumentException("bizServiceStatus cannot be empty!");
        Map<String, Integer> filterSessionServerTypeCouterMap = new HashMap<String, Integer>();
        if (sessionServerStatusMap == null || sessionServerStatusMap.isEmpty()) {
            return filterSessionServerTypeCouterMap;
        }

        for (String ssType : sessionServerStatusMap.keySet()) {
            List<SessionServerStatus> sessionServerStatusList = sessionServerStatusMap.get(ssType);
            filterSessionServerTypeCouterMap.put(ssType, 0);
            for (SessionServerStatus sessionServerStatus : sessionServerStatusList) {
                if (sessionServerStatus.getBizServiceStatus() == bizServiceStatus)
                    filterSessionServerTypeCouterMap.put(ssType, filterSessionServerTypeCouterMap.get(ssType) + 1);
            }
        }

        return filterSessionServerTypeCouterMap;

    }

    public boolean isFailed() {
        return failed;
    }

    public void setFailed(boolean failed) {
        this.failed = failed;
    }

}
